package com.zkjl.protect_business.controller;


import com.zkjl.protect_business.common.ApiResponse;
import com.zkjl.protect_business.common.Status;
import com.zkjl.protect_business.common.SystemLog;
import com.zkjl.protect_business.common.UserThreadLocal;
import com.zkjl.protect_business.dto.*;
import com.zkjl.protect_business.entity.User;
import com.zkjl.protect_business.exception.SecurityException;
import com.zkjl.protect_business.service.UserService;
import com.zkjl.protect_business.utils.JwtUtil;
import com.zkjl.protect_business.utils.ResultUtil;
import com.zkjl.protect_business.vo.PageVO;
import com.zkjl.protect_business.vo.RouteVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author jr
 * @since 2020-06-17
 */
@RestController
@RequestMapping("/users")
@Api(tags = {"用户"})
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    JwtUtil jwtUtil;

    @ApiOperation(value = "新增用户", notes = "新增用户", httpMethod = "POST")
    @PostMapping
    @SystemLog(description = "新增用户")
    public ApiResponse<User> save(@RequestBody UserDTO userDTO) {

        User save = userService.save(userDTO);
        return ApiResponse.ofSuccess(save);
    }


    @ApiOperation(value = "用户列表", notes = "用户列表", httpMethod = "GET")
    @GetMapping
    @SystemLog(description = "查看用户列表")
    public ApiResponse<PageVO<User>> page(PageBaseDTO pageBaseDTO, SortDTO sortDTO) {

        PageImpl page = userService.page(pageBaseDTO, sortDTO);
        ApiResponse apiResponse = ResultUtil.successPagesVO(page);
        return apiResponse;
    }

    @ApiOperation(value = "修改用户", notes = "修改用户", httpMethod = "PUT")
    @PutMapping(value = "{id}")
    @SystemLog(description = "修改用户")
    public ApiResponse<User> update(@RequestBody UserDTO userDTO, @PathVariable String id) {

        User update = userService.update(id, userDTO);
        return ApiResponse.ofSuccess(update);

    }

    @ApiOperation(value = "修改个人信息", notes = "修改个人信息", httpMethod = "PUT")
    @PutMapping(value = "myInfo")
    public ApiResponse<User> updateMyInfo(@RequestBody UpdateUserDTO updateUserDTO) {

        User user = userService.updateMyInfo(updateUserDTO);
        return ApiResponse.ofSuccess(user);

    }


    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", httpMethod = "GET")
    @GetMapping(value = "{id}")
    @SystemLog(description = "查看用户信息")
    public ApiResponse<User> get(@PathVariable String id) {
        User user = userService.get(id);
        return ApiResponse.ofSuccess(user);
    }

    @ApiOperation(value = "修改密码", notes = "修改密码", httpMethod = "PUT")
    @PutMapping(value = "/myInfo/passWord")
    ApiResponse updatePassWord(@RequestBody UpdatePassWordDTO updatePassWordDTO, HttpServletRequest request) {

        Boolean aBoolean = userService.updatePassWord(updatePassWordDTO);
        try {
            // 设置JWT过期
            jwtUtil.invalidateJWT(request);
        } catch (SecurityException e) {
            throw new SecurityException(Status.UNAUTHORIZED);
        }

        return ApiResponse.ofSuccess(aBoolean);

    }


    @ApiOperation(value = "获取个人信息", notes = "获取个人信息", httpMethod = "GET")
    @GetMapping(value = "/myInfo")
    public ApiResponse<User> getMyInfo() {

        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        Object principal = authentication.getPrincipal();

        return ApiResponse.ofSuccess(principal);
    }



    @ApiOperation(value = "删除用户", notes = "删除用户", httpMethod = "DELETE")
    @DeleteMapping(value = "{id}")
    @SystemLog(description = "删除用户")
    public ApiResponse del(@PathVariable String id) {

        Boolean del = userService.del(id);
        return ApiResponse.ofSuccess(del);
    }


    @ApiOperation(value = "获取用户拥有的路由", notes = "获取用户拥有的路由", httpMethod = "GET")
    @GetMapping(value = "/routes")
    ApiResponse getUserRoute() {
        List<RouteVO> userRoute = userService.getUserRoute();
        return ApiResponse.ofSuccess(userRoute);
    }



}
